<?php
	if($_POST["btn_submit"])
	{
		$factor = $_POST["txt_factor"];
		$employee_id = $_POST["txt_employee_id"];
		$wage = $_POST["txt_wage"];
		$payroll_month = $_POST["txt_payroll_month"];
		$payroll_year = $_POST["txt_payroll_year"];

		$basic_salary = $_POST["txt_basic_salary"];
		$holidays = $_POST["txt_holidays"];
		$late = $_POST["txt_late"];
		$leave = $_POST["txt_leave"];
		$absent = $_POST["txt_absent"];

		$debt = $_POST["txt_debt"];
		$saving = $_POST["txt_saving"];
		$lunch = $_POST["txt_lunch"];
		$petrol = $_POST["txt_petrol"];
		$bonus = $_POST["txt_bonus"];
		
		$mysql_connect = mysql_connect('localhost', 'hr_mysql', 'hr_mysql');
		if ($mysql_connect)
		{				
			mysql_select_db('hr_mysql', $mysql_connect);

			$payroll_day = $payroll_year . "-" . $payroll_month  ."-1";
			for($counter = 1; $counter <= 10; $counter++)
			{
				$payroll_value = 1;
				$payroll_amount = 0;
				switch ($counter) 
				{
					case 1:
						$payroll_amount = $basic_salary;
						break;
					case 2:
						$payroll_value = $holidays;
						$payroll_amount = $payroll_value * $wage;
						break;
					case 3:
						$payroll_value = $late;
						$payroll_amount = $payroll_value * $wage;
						break;
					case 4:
						$payroll_value = $leave;
						$payroll_amount = $payroll_value * $wage;
						break;
					case 5:
						$payroll_value = $absent;
						$payroll_amount = $payroll_value * $wage;
						break;
					case 6:
						$payroll_amount = $debt;
						break;
					case 7:
						$payroll_amount = $saving;
						break;
					case 8:
						$payroll_amount = $lunch;
						break;
					case 9:
						$payroll_amount = $petrol;
						break;
					case 10:
						$payroll_amount = $bonus;
						break;
				}		
				
				$mysql_query = 
					"SELECT * " . 
					"FROM hs_hr_emp_payroll_detail " .
					"WHERE payroll_parameter_id = '$counter' AND employee_id = '$employee_id' AND YEAR(payroll_date) = '$payroll_year' AND MONTH(payroll_date) = '$payroll_month'";			
				//echo $mysql_query . '<br/>';
				
				$mysql_result = mysql_query($mysql_query);
				if(!$mysql_result)		
					exit;				
					
				$num_rows = mysql_num_rows($mysql_result);						
				if ($num_rows == 0)
				{
					$mysql_query = 
						"INSERT INTO hs_hr_emp_payroll_detail (payroll_date, employee_id, payroll_parameter_id, payroll_value, payroll_amount, is_locked) " . 
						"VALUES ('$payroll_day', '$employee_id', '$counter', '$payroll_value', '$payroll_amount', 0)";
					//echo $mysql_query . '<br/>';
					mysql_query($mysql_query);
				}
				else
				{
					$mysql_query = 
						"UPDATE hs_hr_emp_payroll_detail " . 
						"SET payroll_date = '$payroll_day', employee_id = '$employee_id', payroll_parameter_id = '$counter', payroll_value = '$payroll_value', payroll_amount = '$payroll_amount', is_locked = '0' " . 
						"WHERE payroll_parameter_id = '$counter' AND employee_id = '$employee_id' AND YEAR(payroll_date) = '$payroll_year' AND MONTH(payroll_date) = '$payroll_month'";
					//echo $mysql_query . '<br/>';
					mysql_query($mysql_query);
				}
				
				mysql_free_result($mysql_result);
			}			
		}
		
		//echo 'done';
		mysql_close($mysql_connect);	

		header("Location: ./payslip.php?emp_id=" . $employee_id . "&factor=" . $factor) ;
	}
?>